
/*!
	\file
	\brief DB with goods informations.

		\author Igor Mironchik (igor.mironchik at gmail dot com).

        Copyright (c) 2010 - 2011 Igor Mironchik

	Permission is hereby granted, free of charge, to any person
	obtaining a copy of this software and associated documentation
	files (the "Software"), to deal in the Software without
	restriction, including without limitation the rights to use,
	copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following
	conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
	OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef ESHOPHELPER__DB_HPP__INCLUDED
#define ESHOPHELPER__DB_HPP__INCLUDED

// Qt include.
#include <QtCore/QString>

#include <QtSql/QSqlDatabase>

// C++ include.
#include <stdexcept>


namespace eShopHelper {

//
// DBException
//

class DBException
	:	public std::runtime_error
{
public:
	explicit DBException( const QString & what );
	virtual ~DBException();

	//! \return What happend as QString.
	const QString & whatAsQString() const;

private:
	//! Reason.
	QString m_what;
}; // class DBException


//
// DB
//

//! DB with goods informations.
class DB {
public:
	//! Initialize the DB.
	void init();
	//! Add new goods to the DB.
	void addGoods( const QString & name, const QString & desc );
	//! \return Is goods exists in the database?
	bool isGoodsExists( const QString & name, const QString & desc );

	//! Check image for the given goods.
	//! \return Path to the image.
	QString checkImage( const QString & name, const QString & desc );
	//! Update record with image in the database.
	void updateImage( const QString & name, const QString & desc,
		const QString & path );
	//! Delete record about image from the database.
	void deleteImage( const QString & name, const QString & desc );

	//! \return Is there the name set by user for this goods.
	QString checkUserName( const QString & name, const QString & desc );
	//! Update user's name of the goods.
	void updateUserName( const QString & name, const QString & desc,
		const QString & userName );

	//! \return Is there the description set by user for this goods.
	QString checkUserDesc( const QString & name, const QString & desc );
	//! Update user's description of the goods.
	void updateUserDesc( const QString & name, const QString & desc,
		const QString & userDesc );

private:
	//! Connection to the DB.
	QSqlDatabase m_connection;
}; // class DB

} /* namespace eShopHelper */

#endif // ESHOPHELPER__DB_HPP__INCLUDED
